<template>
  <div class="login-container">
    <div class="login-box">
      <h1 class="login-title">用户登录</h1>
      <form @submit.prevent="handleLogin" class="login-form">
        <div class="form-group">
          <label for="username">用户名</label>
          <input
            v-model="username"
            id="username"
            type="text"
            required
            placeholder="请输入用户名"
          />
        </div>
        
        <div class="form-group">
          <label for="password">密码</label>
          <input
            v-model="password"
            id="password"
            type="password"
            required
            placeholder="请输入密码"
          />
        </div>

        <button type="submit" class="login-btn">登 录</button>
        
        <div class="login-footer">
          <RouterLink  :to="{path:'/Register'}">立即注册</RouterLink>
          <!-- <p>还没有账号？<a href="../">立即注册</a></p> -->
          <!-- <a href="#" class="forgot-password">忘记密码？</a> -->
        </div>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '../stores/auth'

const username = ref('')
const password = ref('')
const router = useRouter()
const authStore = useAuthStore()

// src/views/Login.vue (script部分优化)
const handleLogin = async () => {
  if (!username.value || !password.value) {
    alert('请输入用户名和密码')
    return
  }
  
  const loginSuccess = authStore.login(username.value, password.value)
  if (loginSuccess) {
    router.push('/todos')
  } else {
    alert('账号或密码错误，请注册账号')
    router.push('/register') // 跳转到注册页
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f5f5f5;
  padding: 20px;
}

.login-box {
  width: 100%;
  max-width: 400px;
  background: #ffffff;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  padding: 30px;
}

.login-title {
  font-size: 24px;
  color: #333;
  text-align: center;
  margin-bottom: 30px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: #666;
  font-size: 14px;
}

.form-group input {
  width: 100%;
  padding: 12px 15px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
  transition: border-color 0.3s;
}

.form-group input:focus {
  outline: none;
  border-color: #409eff;
}

.login-btn {
  width: 100%;
  padding: 12px;
  background-color: #409eff;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s;
  margin-top: 10px;
}

.login-btn:hover {
  background-color: #66b1ff;
}

.login-footer {
  margin-top: 20px;
  text-align: center;
  font-size: 14px;
  color: #999;
}

.login-footer a {
  color: #409eff;
  text-decoration: none;
}

.login-footer a:hover {
  text-decoration: underline;
}

.forgot-password {
  display: block;
  margin-top: 10px;
}
</style>